﻿using VM.CameraSet;
using VM.Common.Helper;
using VM.Core;

namespace VM.Dialogs.ViewModels
{
    public class IpConfigViewModel : NotifyPropertyBase
    {

        private static readonly IpConfigViewModel _instance = new IpConfigViewModel();

        public IpConfigViewModel()
        {
            if (CameraSetViewModel.Ins.SelectedCameraModel != null)
            {
                var IpConfig = CameraSetViewModel.Ins.SelectedCameraModel.GetIpConfig();
                RecommendedIP = IpConfig.IpRange;
                IP = IpConfig.CurrentIP;
                SubnetMask = IpConfig.SubnetMask;
                DefaultGateway = IpConfig.DefaultGateway;
            }
        }

        public static IpConfigViewModel Ins
        {
            get { return _instance; }
        }

        /// <summary>
        /// IP 推荐范围
        /// </summary>
        private string _RecommendedIP = "Info";
        public string RecommendedIP
        {
            get { return _RecommendedIP; }
            set { Set(ref _RecommendedIP, value); }
        }

        /// <summary>
        /// IP
        /// </summary>
        private string _IP;
        public string IP
        {
            get { return _IP; }
            set { Set(ref _IP, value); }
        }

        /// <summary>
        /// 子网掩码
        /// </summary>
        private string _SubnetMask;
        public string SubnetMask
        {
            get { return _SubnetMask; }
            set { Set(ref _SubnetMask, value); }
        }

        /// <summary>
        /// 子网掩码
        /// </summary>
        private string _DefaultGateway;
        public string DefaultGateway
        {
            get { return _DefaultGateway; }
            set { Set(ref _DefaultGateway, value); }
        }

        [Newtonsoft.Json.JsonIgnore]
        private CommandBase _ConfigCommand;
        [Newtonsoft.Json.JsonIgnore]
        public CommandBase ConfigCommand
        {
            get
            {
                if (_ConfigCommand == null)
                {
                    _ConfigCommand = new CommandBase((obj) =>
                    {
                        ConfigIP();
                    });
                }
                return _ConfigCommand;
            }
        }

        private void ConfigIP()
        {
            if (CameraSetViewModel.Ins.SelectedCameraModel != null)
            {
                CameraSetViewModel.Ins.ButtonOperateCommand.Execute("Disconnect");
                IPConfig iPConfig = new IPConfig()
                {
                    CurrentIP = IP,
                    SubnetMask = SubnetMask,
                    DefaultGateway = DefaultGateway,
                };
                CameraSetViewModel.Ins.SelectedCameraModel.SetIpConfig(iPConfig);
                var Ipconfig = CameraSetViewModel.Ins.SelectedCameraModel.GetIpConfig();
                CameraSetViewModel.Ins.SelectedCameraModel.CameraIP = Ipconfig.CurrentIP;
            }
        }
    }
}
